package com.example.module_course.pages

import android.content.Intent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import com.example.module_course.R
import com.example.module_course.activity.MoreCourseActivity
import com.example.module_course.common.VerticalNestedScrollView
import com.example.module_course.common.click
import com.example.module_course.common.rememberNestedScrollViewState
import com.example.module_course.utils.bannerVideoId
import com.example.module_course.utils.openVideo
import com.example.module_course.utils.popularVideoList
import com.fenghuajueli.libbasecoreui.baseswitch.PublicFunction
import com.fenghuajueli.module_user.ui.widget.noRippleClickable


@Preview
@Composable
fun CourseScreen() {
    val context = LocalContext.current  // 获取当前的Context


    Box(modifier = Modifier.background(Color.White)) {

        Image(
            modifier = Modifier.wrapContentSize().padding(top = 40.dp, start = 20.dp),
            painter = painterResource(id = R.mipmap.title_2),
            contentDescription = "",
            contentScale = ContentScale.FillWidth,
            alignment = Alignment.TopCenter
        )

        Column(
            modifier = Modifier
                .fillMaxSize()
                .statusBarsPadding()
        ) {

            Box(
                modifier = Modifier
                    .clipToBounds()
                    .padding(top = 80.dp)
            ) {

            }
            Column {

                Row(
                    horizontalArrangement = Arrangement.Center,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(start = 16.dp, top = 30.dp, end = 16.dp)
                ) {
                    Image(
                        painter = painterResource(id = R.mipmap.card_jck),
                        contentDescription = null,
                        modifier = Modifier
                            .weight(1f)
                            .noRippleClickable {

                                context.startActivity(
                                    Intent(context, MoreCourseActivity::class.java)
                                        .putExtra("courseType", "基础课")
                                )

                            }
                    )

                    Image(
                        painter = painterResource(id = R.mipmap.card_jjk),
                        contentDescription = null,
                        modifier = Modifier.padding(start = 10.dp)
                            .weight(1f)
                            .noRippleClickable {
                                context.startActivity(
                                    Intent(context, MoreCourseActivity::class.java)
                                        .putExtra("courseType", "进阶课")
                                )
                            }
                    )
                }

                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 20.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(
                        text = "人气好课", style = TextStyle(
                            color = Color(0xFF333333),
                            fontSize = 18.sp,
                            fontWeight = FontWeight.SemiBold
                        ), modifier = Modifier.padding(start = 20.dp)
                    )
                }

                CourseItemView()
            }
        }
    }
}

@Composable
fun CourseItemView() {
    LazyColumn(
        verticalArrangement = Arrangement.spacedBy(16.dp),
        contentPadding = PaddingValues(20.dp),
        modifier = Modifier.fillMaxSize()
    ) {
        items(popularVideoList) {
            ConstraintLayout(modifier = Modifier
                .fillMaxWidth()
                .noRippleClickable {
                    if (PublicFunction.checkCanUsedByPosition(2, true)) {
                        it.url.openVideo()
                    }
                }) {
                val (titleRef, coverRef, watchRef, starRef) = createRefs()

                Image(
                    painter = painterResource(id = it.imgRes),
                    contentDescription = null,
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .size(132.dp, 82.dp)
                        .clip(RoundedCornerShape(10.dp))
                        .constrainAs(coverRef) {
                            start.linkTo(parent.start)
                            top.linkTo(parent.top)
                            bottom.linkTo(parent.bottom)
                        })

                Text(
                    text = it.name,
                    style = TextStyle(
                        color = Color(0xFF333333),
                        fontSize = 13.sp,
                        fontWeight = FontWeight.Normal
                    ),
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier.constrainAs(titleRef) {
                        start.linkTo(coverRef.end, 20.dp)
                        end.linkTo(parent.end)
                        top.linkTo(coverRef.top, 12.dp)
                        width = Dimension.fillToConstraints
                    }
                )

                Text(
                    text = "${(3000..8000).random()}人已观看",
                    style = TextStyle(
                        color = Color(0xFF999999),
                        fontSize = 12.sp,
                        fontWeight = FontWeight.Normal
                    ),
                    textAlign = TextAlign.Center,
                    modifier = Modifier
                        .constrainAs(watchRef) {
                            start.linkTo(titleRef.start)
                            bottom.linkTo(coverRef.bottom, 14.dp)
                        }
                )

                Image(
                    painter = painterResource(id = R.mipmap.btn_ksyd),
                    contentDescription = null,
                    contentScale = ContentScale.Crop,
                    modifier = Modifier.padding(start = 20.dp)
                        .constrainAs(starRef) {
                            start.linkTo(watchRef.end)
                            top.linkTo(watchRef.top)
                            bottom.linkTo(watchRef.bottom)
                        })
            }
        }
    }
}